package clientMain;

import lib.StreamSocket;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @file Receiver.java
 * 
 * @version 0.1
 * @author Chris Sewell 10008342
 */
public class Receiver extends Thread {

	StreamSocket streamServer;
	String serverOutput;
	boolean debug = true;

	public Receiver(StreamSocket serverStream, boolean debug) {
		this.streamServer = serverStream;
		this.debug = debug;
	}

	@Override
	public void run() {
		while (streamServer.isOpen()) {
			serverOutput = streamServer.recieveString();

			if (serverOutput == null) {// If null string received kill
										// connection
				// from server
				try {
					streamServer.kill();
				} catch (IOException ex) {
					Logger.getLogger(Receiver.class.getName()).log(
							Level.SEVERE, null, ex);
				}
				return;
			}
			if (!serverOutput.equals("")) {
				if (debug == true) {// DEBUG MESSAGE
					System.out.print("Received from: "
							+ streamServer.displayDetail() + "\n"
							+ serverOutput + "\n");
				} else {
					serverOutput = Parser.parseResponse(serverOutput);
					System.out.print(serverOutput + "\n");// Print parsed
															// header
				}
			}
		}
	}
}
